package com;

import com.entity.Student;
import com.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class Test {
    /**
     * 启动MyBatis去加载配置文件 1.java.io.Reader 2.Resources 3.SqlSessionFactory
     */
    private static Reader reader;
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            reader = Resources.getResourceAsReader("mybatis.xml");//读取指定的配置文件 生成jdbc等mybatis需要的数据库信息
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);//根据读取的信息构造一个mybatis的工厂类
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SqlSession sqlSession = sqlSessionFactory.openSession();//test类的静态代码块已经初始化好了数据库连接,使用创建好的工厂打开一次连接
         StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);//将mapper接口的定义送入sql工厂加工，工厂根据studentMapper.xml
        //生成匿名实现类
//         List<Student> list = studentMapper.getAllStudents();//匿名实现类的方法调用得到返回值
//        list.forEach(System.out::println);
       // Student student = studentMapper.getStudentById("002");
       // System.out.println(student);
    }
}
